<?php

namespace App\Http\Controllers\Admin\Business\Service;

use App\Http\Controllers\Admin\Business\Repository\ComboOrder as ComboOrderRepository;
use App\Member;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ComboOrder
{
    use SoftDeletes;
    /**
     * 发布状态 1启用 2禁用
     */
    const ON = 1;
    const OFF = 2;
    /**
     * 返利类型
     */
    const RATE_TYPE_SYS = 1;
    const RATE_TYPE_USER = 2;

    /**
     * 活动方式
     */
    const REBATE = 1;
    const REDUCE = 2;

    /**
     * Http请求
     * @var $request
     */
    private $request;
    private $repository;

    public function __construct ()
    {
        $this->repository = new ComboOrderRepository();
    }
    //读操作
    public function getComboOrder($id){
        if(Auth::user()&&Auth::user()->can('comboAllOrder')){
            return $this->repository->withTrashed()->with(['combo','activity'])->find($id);
        }else{
            return $this->repository->with(['combo','activity'])->find($id);
        }
    }

    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request,$order=false){
        $this->request = $request;
        $model=$this->repository->with(['combo','activity','member']);
        if(Auth::user()->can('comboAllOrder')){
            $model = $model->withTrashed();
        }

        //时间

        $model = $model->join('pay_orders','pay_orders.out_trade_no','=','combo_orders.outTradeNo')
            ->join('members','members.id','=','combo_orders.member_id');
        $model = $model->where('pay_orders.is_pay',1);
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->where('pay_orders.created_at','>',$start_time);
            $model=$model->where('pay_orders.created_at','<',$end_time);


        }
        if (isset($this->request->parent_name)&&$this->request->parent_name!=''){
          $parent=  Member::where('nickname',$this->request->parent_name)->first();
          $model=$model->where('members.pid',$parent->id);
        }
        if (isset($this->request->outTradeNo)&&$this->request->outTradeNo!=''){
          $model=$model->where('combo_orders.outTradeNo',$this->request->outTradeNo);
        }
        if (isset($this->request->nickname)&&$this->request->nickname!=''){
          $parent=  Member::where('nickname',$this->request->nickname)->first();
          $model=$model->where('members.id',$parent->id);
        }
        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }else{
            $model = $model->orderBy('id','DESC');
        }
        $model = $model->select('combo_orders.*','members.nickname');

        $model=$model->paginate($this->request->limit);
        if($model){
            $model=$model->toArray();
        }
        foreach ($model['data'] as $k=>&$v){
            $v['combo_name'] = isset($v['combo']['title'])?$v['combo']['title']:'未选择套餐';
            $v['activity_name'] = isset($v['activity']['title'])?$v['activity']['title']:'未参与活动';
            $v['member_name'] = isset($v['member']['nickname'])?$v['member']['nickname']:'匿名用户';
            $v['mobile']=$v['member']['mobile'];
            $v['is_send']=isset($v['is_send'])&&$v['is_send']==1 ? '已发放':'未发放';
            $member= Member::where('id',$v['member_id'])->first();
            if($member){
                $parent= Member::where('id',$member->pid)->first();
                if($parent){
                    $v['parent']=$parent;
                }else{
                    $v['parent']=[];
                }
            }
        }
        return $model;
    }

    public function exportList(Request $request,$order=false){
        $this->request = $request;
        $model=$this->repository->with(['combo','activity','member']);
    /*    if(Auth::user()->can('comboAllOrder')){
            $model = $model->withTrashed();
        }*/

        $model = $model->join('pay_orders','pay_orders.out_trade_no','=','combo_orders.OutTradeNo')
            ->join('members','members.id','=','combo_orders.member_id');
        $model = $model->where('pay_orders.is_pay',1);
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->where('pay_orders.created_at','>',$start_time);
            $model=$model->where('pay_orders.created_at','<',$end_time);
        }
        if (isset($this->request->parent_name)&&$this->request->parent_name!=''){
            $parent=  Member::where('nickname',$this->request->parent_name)->first();
            $model=$model->where('members.pid',$parent->id);
        }
        if (isset($this->request->nickname)&&$this->request->nickname!=''){
            $parent=  Member::where('nickname',$this->request->nickname)->first();
            $model=$model->where('members.id',$parent->id);
        }

        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }else{
            $model = $model->orderBy('id','DESC');

        }
        $model = $model->select('combo_orders.*');
        $model = $model->get();
        if($model){
            $model=$model->toArray();
        }
        $cellData = [
            [
                'id',
                '套餐',
                '活动',
                '推广人',
                '数量',
                '是否发放门票',
                '手机号',
                '用户昵称',
                '金额（￥）',
                '支付时间'
            ]
        ];
        foreach ($model as $k=>&$v){
            $member= Member::where('id',$v['member_id'])->first();
            if($member){
                $parent= Member::where('id',$member->pid)->first();
                if($parent){
                    $parent_name=$parent->nickname;
                }else{
                    $parent_name='无推广人';
                }
            }
            $temp_data = [
                $v['id'],
                isset($v['combo']['title'])?$v['combo']['title']:'未选择套餐',
                isset($v['activity']['title'])?$v['activity']['title']:'未参与活动',
                preg_replace('/[\xf0-\xf7].{3}/',' ',$parent_name),
                $v['total_num'],
                $v['is_send']==1 ? '已发放' : '未发放',
                $v['member']['mobile'],
                preg_replace('/[\xf0-\xf7].{3}/',' ',isset($v['member']['nickname'])?$v['member']['nickname']:'匿名用户'),
                $v['price'],

                $v['created_at']
            ];
            $cellData[] = $temp_data;
        }
       // dd($cellData);
        return $cellData;
    }
    public function exportList1($request,$order=false){
        $this->request = $request;
        $model=$this->repository->with(['combo','activity','member']);

        //时间
        if(isset($this->request['start_time'])){

            $start_time=trim($this->request['start_time']);
            $end_time=trim($this->request['end_time']);
            $model=$model->whereBetween('combo_orders.created_at',[$start_time,$end_time]);
        }
        $model = $model->join('pay_orders','pay_orders.out_trade_no','=','combo_orders.OutTradeNo');
        $model = $model->where('pay_orders.is_pay',1);
        if($order==true){
            $model = $model->orderBy('sort','ASC');
        }else{
            $model = $model->orderBy('id','DESC');
        }
        $model = $model->select('combo_orders.*');
        $model = $model->get();
        if($model){
            $model=$model->toArray();
        }
        $cellData = [
            [
                'id',
                '套餐',
                '活动',
                '推广人',
                '手机号',
                '用户昵称',
                '金额（￥）',
                '支付时间'
            ]
        ];
        foreach ($model as $k=>&$v){
            $member= Member::where('id',$v['member_id'])->first();

            if($member){
                $parent= Member::where('id',$member->pid)->first();
                if($parent){
                    $parent_name=$parent->nickname;
                }else{
                    $parent_name='无推广人';
                }
            }
            $temp_data = [
                $v['id'],
                isset($v['combo']['title'])?$v['combo']['title']:'未选择套餐',
                isset($v['activity']['title'])?$v['activity']['title']:'未参与活动',
                preg_replace('/[\xf0-\xf7].{3}/',' ',$parent_name),
                $v['member']['mobile'],
                preg_replace('/[\xf0-\xf7].{3}/',' ',isset($v['member']['nickname'])?$v['member']['nickname']:'匿名用户'),
                $v['price'],
                $v['created_at']
            ];
            $cellData[] = $temp_data;
        }
        // dd($cellData);
        return $cellData;
    }
    //写操作
    public function getListCount(Request $request){
        $this->request = $request;
        $model=$this->repository->with(['combo','activity','member']);
       if(Auth::user()->can('comboAllOrder')){
            $model = $model->withTrashed();
        }
        //时间
        if(isset($this->request->searchTime)&&$this->request->searchTime!=''){
            $timeArray=explode('~',$this->request->searchTime);
            $start_time=trim($timeArray[0]);
            $end_time=trim($timeArray[1]);
            $model=$model->whereBetween('created_at',[$start_time,$end_time]);
        }
        $model = $model->join('pay_orders','pay_orders.out_trade_no','=','combo_orders.OutTradeNo')
            ->join('members','members.id','=','combo_orders.member_id');
        $model = $model->where('pay_orders.is_pay',1);
        if (isset($this->request->parent_name)&&$this->request->parent_name!=''){
            $parent=  Member::where('nickname',$this->request->parent_name)->first();
            $model=$model->where('members.pid',$parent->id);
        }
        if (isset($this->request->nickname)&&$this->request->nickname!=''){
            $parent=  Member::where('nickname',$this->request->nickname)->first();
            $model=$model->where('members.id',$parent->id);
        }
      return  $model->sum('price');
    }
    /**
     * 隐藏订单
     * @param array $id
     */
    public function hide($id = []){
            if($id == []){

            }else{
                if(is_array($id)){
                    $this->repository->whereIn('id',$id)->delete();
                }else{
                    $this->repository->where('id',$id)->delete();

                }
            }
    }

    /**
     * 显示订单
     */
    public function show($id){
        if($id == []){

        }else{
            if(is_array($id)){
                $this->repository->withTrashed()->whereIn('id',$id)->restore();
            }else{
                $this->repository->withTrashed()->where('id',$id)->restore();

            }
        }
    }

}
